//
//  KLMyWalletTableViewController.m
//  KISSLOCK
//
//  Created by 陈宾鹏 on 16/6/23.
//  Copyright © 2016年 陈宾鹏. All rights reserved.
//

#import "KLMyWalletTableViewController.h"
#import "KLHelp.h"
#import "KLHttpAPI.h"
#import "FMDBTool.h"
#import "Masonry.h"
#import "HemaConst.h"
#import "KLDetailBillTableViewController.h"
#import "KLRechargeViewController.h"
#import "KLCrashViewController.h"
#import "KLSetPayPasswordViewController.h"
#import "KLManagePayPwdTableViewController.h"

@interface KLMyWalletTableViewController ()
{
    UILabel *balanceNumber;
}
@end

@implementation KLMyWalletTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGB_UI_COLOR(242, 242, 242);
    self.title = @"我的钱包";
    //初始化导航栏
    [self naveSet];
    //初始化主界面
    [self uiSet];
    [KLHttpAPI requestBalanceWithCompletionHandler:^(MKNetworkOperation *operation) {
        NSLog(@"%@",operation.responseJSON);
        NSString *isSuccess = [operation.responseJSON objectForKey:@"success"];
        if(isSuccess.integerValue == 1){
            NSString *infor = [operation.responseJSON objectForKey:@"infor"];
            balanceNumber.text = infor;
            [[NSUserDefaults standardUserDefaults] setObject:infor forKey:@"feeaccount"];
        }
    } errorHandler:^(NSString *error) {
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"myWallet"];
    cell.textLabel.font = [UIFont systemFontOfSize:18];
    [tableView setSeparatorColor:[UIColor clearColor]];

    if (indexPath.section == 0){
        cell.textLabel.text = @"充值";
    }else if (indexPath.section == 1){
        cell.textLabel.text = @"提现";
    }else if (indexPath.section == 2){
        cell.textLabel.text = @"支付密码管理";
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *isExitPaypwd = [[NSUserDefaults standardUserDefaults] objectForKey:@"isPaypwdExit"];
    if(isExitPaypwd.integerValue == 0){
        KLSetPayPasswordViewController *setPayPasswordVC = [[KLSetPayPasswordViewController alloc]init];
        [self presentViewController:setPayPasswordVC animated:YES completion:nil];
        return ;
    }
    if(indexPath.section == 0){//充值
        KLRechargeViewController *reChargeVC = [[KLRechargeViewController alloc]init];
        [self.navigationController pushViewController:reChargeVC animated:YES];
    }else if (indexPath.section == 1){//提现
        KLCrashViewController *crashVC = [[KLCrashViewController alloc]init];
        [self.navigationController pushViewController:crashVC animated:YES];
    }else if (indexPath.section == 2){
        KLManagePayPwdTableViewController *managePayPwdVC = [[KLManagePayPwdTableViewController alloc]init];
        [self.navigationController pushViewController:managePayPwdVC animated:YES];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section == 0){
        return 0;
    }else{
        return 5;
    }
}

#pragma mark - 初始化导航栏
-(void)naveSet{
    self.navigationController.navigationBarHidden = NO;
    [KLHelp navSet:self doBackHandeler:^(UIButton *backButton) {
        [backButton addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
    }];
    //右侧明细按钮
    UIButton *detailButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 20)];
    [detailButton setTitle:@"明细" forState:UIControlStateNormal];
    [detailButton addTarget:self action:@selector(detailShow:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:detailButton];
}-(void)doBack:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}-(void)detailShow:(id)sender{
    KLDetailBillTableViewController *detailBillVC = [[KLDetailBillTableViewController alloc]init];
    [self.navigationController pushViewController:detailBillVC animated:YES];
}

#pragma mark - 初始化主界面
-(void)uiSet{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UI_View_Width, 145)];
    headView.backgroundColor = RGB_UI_COLOR(230, 145, 56);
    self.tableView.tableHeaderView = headView;
    
    UILabel *balanceName = [[UILabel alloc]init];
    balanceName.text = @"余额账户(元)";
    balanceName.textColor = RGB_UI_COLOR(217, 217, 217);
    balanceName.font = [UIFont systemFontOfSize:14];
    [headView addSubview:balanceName];
    [balanceName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(18);
        make.top.mas_equalTo(15);
    }];
    
    balanceNumber = [[UILabel alloc]init];
    balanceNumber.textColor = [UIColor whiteColor];
    balanceNumber.font = [UIFont systemFontOfSize:65];
    [headView addSubview:balanceNumber];
    [balanceNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(45);
    }];
}

-(void)viewWillAppear:(BOOL)animated{
    balanceNumber.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"feeaccount"];
}

@end
